/**
 * 区域设置 列表页
 *
 * 作者 admin
 * 日期 2024-07-31 14:35:47
 */
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        validate = layui.validate,
        $view = $('#febs-baseUserScoreRecord'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#baseUserScoreRecord-table-form'),
        tableIns;

    form.verify(validate);
    form.render();

    initTable();
    initDate();

    function initDate() {
        //初始化日期控件
        laydate.render({
            elem: '#operateDate',
        })
    }

    function initTable() {
        tableIns = febs.table.init({
            elem: $view.find('table'),
            id: 'baseUserScoreRecordTable',
            url: ctx + 'baseUserScoreRecord/list',
            height: getTableHeight(),
            where: getQueryParams(),
            skin: '',
            cols: [[
                {type: 'numbers', title: '序号'},
                {field: 'realName', title: '用户姓名', width: 120,align:'center'},
                {field: 'userNo', title: '用户工号', width: 120,align:'center'},
                {field: 'deptName', title: '所属部门', width: 120,align:'center'},
                {field: 'dataSource', title: '积分来源', width: 120 ,align:'center',templet:function (d){
                    let dataSourceName = '--'
                    switch (d.dataSource) {
                        case '1':
                            dataSourceName = '事故管理';
                            break;
                        case '2':
                            dataSourceName = '违章管理';
                            break;
                        case '3':
                            dataSourceName = '隐患管理';
                            break;
                        case '4':
                            dataSourceName = '安全检查';
                            break;
                        case '5':
                            dataSourceName = '激励管理';
                            break;
                        case '99':
                            dataSourceName = '积分兑换';
                            break;
                        default:
                            break;
                    }
                    return dataSourceName;
                }},
                {field: 'operateType', title: '积分获取/消耗', width: 130 ,align:'center',templet:function (d){
                        if(d.operateType === '1'){
                            return  '<span class="layui-badge febs-tag-green" style="width: 22px;line-height: 15px">+</span>';
                        }else if (d.operateType === '2') {
                            return  '<span class="layui-badge febs-tag-red" style="width: 22px;line-height: 15px">-</span>';
                        }
                    }},
                {field: 'operateScore', title: '获取/消耗积分', width: 130,align:'center'},
                {field: 'beforeScore', title: '更新前积分', width: 120,align:'center'},
                {field: 'afterScore', title: '更新后积分', width: 120,align:'center'},
                {field: 'operateDate', title: '操作时间', width: 180,align:'center'},
                {field: 'remark', title: '备注', minWidth: 120},

            ]],
        });
    }

    function reloadPage(curr) {
        var _curr = $view.find(".layui-laypage-em").next().html();
        if (!!curr) {
            _curr = curr;
        }
        tableIns.reload({where: getQueryParams(), page: {curr: _curr}});
    }

    $query.on('click', function () {
        reloadPage(1);
    });

    function getQueryParams() {
        return {
            // tenantId: $searchForm.find("select[name='tenantId']").find("option:selected").val(),
            tenantId: currentTenantId,
            schedulingDate: $searchForm.find('input[name="operateDate"]').val(),
            operateType: $searchForm.find("select[name='operateType']").find("option:selected").val(),
            invalidate_ie_cache: new Date()
        };

    }

    function getTableHeight() {
        return commonJS.calcTableHeight(32);
    }

    $(window).resize(function () {
        tableIns.reload({height: getTableHeight()});
    });
});